package com.hn.license;

import de.schlichtherle.license.*;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Scanner;
import java.util.prefs.Preferences;

/**
 * VerifyLicense
 * @author melina
 */
public class InstallLicense {
    //common param
    private static String PUBLICALIAS = "";
    private static String STOREPWD = "";
    private static String SUBJECT = "";
    private static String licPath = "";
    private static String pubPath = "";

    /**
     * 验证证书
     * @return
     */
    public boolean verify() {
        LicenseManager licenseManager = LicenseManagerHolder.getLicenseManager(initLicenseParams());
        Scanner scanner = new Scanner(System.in);
        try {
            licenseManager.verify();
            System.out.println("验证证书成功,按任意键退出...");
            scanner.nextLine();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("证书验证失效,按任意键退出...");
            scanner.nextLine();
            return false;
        }
        return true;
    }

    /**
     * 安装证书
     * @return
     */
    public boolean install() {
        LicenseManager licenseManager = LicenseManagerHolder.getLicenseManager(initLicenseParams());
        Scanner scanner = new Scanner(System.in);
        try {
            //licPath为证书文件文字
            InputStream inputStream = getClass().getResourceAsStream(licPath);
            File licFile = new File(System.getProperty("user.dir") + File.separator + licPath);
            FileUtils.copyInputStreamToFile(inputStream, licFile);
            licenseManager.install(licFile);
            //安装后将文件删除
            licFile.delete();
            System.out.println("证书安装成功,按任意键退出...");
            scanner.nextLine();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("证书安装失败,按任意键退出...");
            scanner.nextLine();
            return false;
        }
        return false;
    }

    /**
     * 卸载证书
     * @return
     */
    public boolean unInstall() {
        LicenseManager licenseManager = LicenseManagerHolder.getLicenseManager(initLicenseParams());
        Scanner scanner = new Scanner(System.in);
        try {
            licenseManager.uninstall();
            System.out.println("证书卸载成功,按任意键退出...");
            scanner.nextLine();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("证书卸载失败,按任意键退出...");
            scanner.nextLine();
            return false;
        }
        return true;
    }


    /**
     * 设置参数
     * @param propertiesPath
     */
    public void setParam(String propertiesPath) {
        // 获取参数
        Properties prop = new Properties();
        InputStream in = getClass().getResourceAsStream(propertiesPath);
        try {
            prop.load(in);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        PUBLICALIAS = prop.getProperty("PUBLICALIAS");
        STOREPWD = prop.getProperty("STOREPWD");
        SUBJECT = prop.getProperty("SUBJECT");
        licPath = prop.getProperty("licPath");
        pubPath = prop.getProperty("pubPath");
    }

    /**
     * 返回验证证书需要的参数
     * @return
     */
    private static LicenseParam initLicenseParams() {
        Preferences preference = Preferences.systemRoot();
        CipherParam cipherParam = new DefaultCipherParam(STOREPWD);

        KeyStoreParam privateStoreParam = new DefaultKeyStoreParam(InstallLicense.class, pubPath, PUBLICALIAS, STOREPWD, null);
        LicenseParam licenseParams = new DefaultLicenseParam(SUBJECT,preference, privateStoreParam, cipherParam);
        return licenseParams;
    }


}
